package com.sunvua.water_mark

import android.content.res.Resources
import android.graphics.*
import android.view.View
import java.io.ByteArrayOutputStream


object ImageUtil {
    /**
     * 设置水印图片在左上角
     *
     * @param src
     * @param watermark
     * @param paddingLeft
     * @param paddingTop
     * @return
     */
    fun createWaterMaskLeftTop(
        src: Bitmap?,
        watermark: Bitmap,
        paddingLeft: Int,
        paddingTop: Int
    ): Bitmap? {
        return createWaterMaskBitmap(
            src,
            watermark,
            dp2px(paddingLeft.toFloat()),
            dp2px(paddingTop.toFloat())
        )
    }

    /**
     * 设置水印图片在右下角
     *
     * @param src
     * @param watermark
     * @param paddingRight
     * @param paddingBottom
     * @return
     */
    fun createWaterMaskRightBottom(
        src: Bitmap,
        watermark: Bitmap,
        paddingRight: Int,
        paddingBottom: Int
    ): Bitmap? {
        return createWaterMaskBitmap(
            src,
            watermark,
            src.width - watermark.width - dp2px(paddingRight.toFloat()),
            src.height - watermark.height - dp2px(paddingBottom.toFloat())
        )
    }

    /**
     * 设置水印图片到右上角
     *
     * @param src
     * @param watermark
     * @param paddingRight
     * @param paddingTop
     * @return
     */
    fun createWaterMaskRightTop(
        src: Bitmap,
        watermark: Bitmap,
        paddingRight: Int,
        paddingTop: Int
    ): Bitmap? {
        return createWaterMaskBitmap(
            src,
            watermark,
            src.width - watermark.width - dp2px(paddingRight.toFloat()),
            dp2px(paddingTop.toFloat())
        )
    }

    /**
     * 设置水印图片到左下角
     *
     * @param src
     * @param watermark
     * @param paddingLeft
     * @param paddingBottom
     * @return
     */
    fun createWaterMaskLeftBottom(
        src: Bitmap,
        watermark: Bitmap,
        paddingLeft: Int,
        paddingBottom: Int
    ): Bitmap? {
        return createWaterMaskBitmap(
            src,
            watermark,
            dp2px(paddingLeft.toFloat()),
            src.height - watermark.height - dp2px(paddingBottom.toFloat())
        )
    }

    /**
     * 设置水印图片到中间
     *
     * @param src
     * @param watermark
     * @return
     */
    fun createWaterMaskCenter(src: Bitmap, watermark: Bitmap): Bitmap? {
        return createWaterMaskBitmap(
            src,
            watermark,
            (src.width - watermark.width) / 2f,
            (src.height - watermark.height) / 2f
        )
    }

    /**
     *
     * @param src Bitmap 背景图片
     * @param context String 水印文字
     * @param textSize Int 文字大小
     */
    fun addTextWatermark(src:Bitmap,content:String,textSize:Float,color:Int):Bitmap? {

        if (isEmptyBitmap(src) || content == null) return null
        val ret: Bitmap = src.copy(src.getConfig(), true)
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        val canvas = Canvas(ret)
        paint.color = color
        paint.textSize = textSize
        val bounds = Rect()
        paint.getTextBounds(content, 0, content.length, bounds)
        canvas.drawText(content,0f,0f,paint)
        src.recycle();
        return ret
    }
    private fun isEmptyBitmap(src: Bitmap?): Boolean {
        return src == null || src.width == 0 || src.height == 0
    }

    private fun createWaterMaskBitmap(
        src: Bitmap?,
        watermark: Bitmap,
        paddingLeft: Float,
        paddingTop: Float
    ): Bitmap? {
        if (src == null) {
            return null
        }
        val width = src.width
        val height = src.height
        //创建一个bitmap
        val newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) // 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布
        val canvas = Canvas(newb)
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(src, 0.0f, 0.0f, null)
        //在画布上绘制水印图片
        canvas.drawBitmap(watermark, paddingLeft, paddingTop, null)
        // 保存
        canvas.save()
        // 存储
        canvas.restore()
        return newb
    }

    /**
     * 将View转成Bitmap
     *
     * @param view
     * @return
     */
    fun viewToBitmap(view: View): Bitmap? {
        view.setDrawingCacheEnabled(true)
        view.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight())
        view.buildDrawingCache()
        return view.getDrawingCache()
    }

    /**
     * dp转pix
     *
     * @param dp
     * @return
     */
    fun dp2px(dp: Float): Float {
        val scale: Float = Resources.getSystem().getDisplayMetrics().density
        return (dp * scale + 0.5f)
    }

    /**
     * 把Bitmap转Byte
     */
    fun BitmapToBytes(bm: Bitmap): ByteArray? {
        val baos = ByteArrayOutputStream()
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos)
        return baos.toByteArray()
    }

    fun BytesToBitmap(bytes: ByteArray?): Bitmap? {
        return if (bytes != null) {
            BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
        } else null
    }
}